package com.oocl.project5.doublelinklist;




public class Node<T> {
	private Node<T> per;
	private Node<T> next;
	private T data;
	public Node<T> getPer() {
		return per;
	}
	public void setPer(Node<T> per) {
		this.per = per;
	}
	public Node<T> getNext() {
		return next;
	}
	public void setNext(Node<T> next) {
		this.next = next;
	}
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	
	public Node<T> add(Node<T> headPointer,T data){
		Node<T> curPointer = headPointer;
		if(curPointer == null)
			throw new NullPointerException();
			//return null;
		Node<T> newPointer = new Node<T>();
		while(curPointer.getNext()!=null){
			curPointer = curPointer.getNext();
		}
		curPointer.setNext(newPointer);
		newPointer.setPer(curPointer);
		newPointer.setData(data);
		return headPointer;
	}
	
	public Node<T> delete(Node<T> headPointer,T data){
		Node<T> curPointer = headPointer;
		if(headPointer == null)
			throw new NullPointerException();
			//return null;
		while(curPointer!=null){
			if(curPointer.getData()==data){
				if(curPointer.getPer()==null){
					headPointer = curPointer.getNext();
					headPointer.setPer(null);
					curPointer.setNext(null);
					break;
				}else if(curPointer.getNext()==null){
					curPointer.getPer().setNext(null);
					curPointer.setPer(null);
					break;
				}else {
					curPointer.getPer().setNext(curPointer.getNext());
					curPointer.getNext().setPer(curPointer.getPer());
					curPointer.setPer(null);
					curPointer.setNext(null);
					break;
				}
			}
			curPointer = curPointer.getNext();
		}
		return headPointer;
	}
	
	public Node<T> updata(Node<T> headPointer,T data,int n){
		Node<T> curPointer = headPointer;
		if(curPointer == null)
			throw new NullPointerException();
		int length = headPointer.length(headPointer);
		int l = 0;
		while(curPointer!=null && 0<=n && n<length){
			if(n == l){
				curPointer.setData(data);;
			}
			curPointer = curPointer.getNext();
			l++;
		}
		return headPointer;
	}
	
	public Node<T> look(Node<T> headPointer,T data){
		return headPointer;
	}
	
	public Node<T> sort(Node<T> headPointer,T data){
		return headPointer;
	}
	public int length(Node<T> headPointer){
		Node<T> curPointer = headPointer;
		int length = 0;
		while(curPointer != null){
			length++;
			curPointer = curPointer.getNext();
		}
		return length;
	}
	public Node(T data) {
		// TODO Auto-generated constructor stub
		this.data = data;
		this.per = null;
		this.next = null;
	}
	public Node() {
		// TODO Auto-generated constructor stub
		this.data = null;
		this.per = null;
		this.next = null;
	}
}
